<?php
class RsvpsController extends AppController {

	var $name = 'Rsvps';

	function index() {
		$this->Rsvp->recursive = 0;
		$this->set('rsvps', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid rsvp', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('rsvp', $this->Rsvp->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Rsvp->create();
			if ($this->Rsvp->save($this->data)) {
				$this->Session->setFlash(__('The rsvp has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The rsvp could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Rsvp->User->find('list');
		$events = $this->Rsvp->Event->find('list');
		$this->set(compact('users', 'events'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid rsvp', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Rsvp->save($this->data)) {
				$this->Session->setFlash(__('The rsvp has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The rsvp could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Rsvp->read(null, $id);
		}
		$users = $this->Rsvp->User->find('list');
		$events = $this->Rsvp->Event->find('list');
		$this->set(compact('users', 'events'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for rsvp', true));
			$this->redirect(array('action'=>'index'));
		}
                  if($this->Rsvp->Review->deleteAll(array('review.rsvp_id'=> $id))){

                    if ($this->Rsvp->delete($id)) {
                        	$this->Session->setFlash(__('Rsvp deleted', true));
                            $this->redirect(array('action'=>'index'));
                    }

                  }
		$this->Session->setFlash(__('Rsvp was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

        function rsvp_action($id = null, $ev_type = 'local_events') {

            if (!$id) {
			$this->Session->setFlash(__('Invalid id for event', true));
			$this->redirect(array('controller' => 'events','action'=>'index'));
            }

            $attendance = $this->data['Event']['attending'];
            $n_guests = $this->data['Event']['n_guests'];

            $user = $this->Session->read('user');

            $user_id = $user['id'];
            
            $event = $this->Rsvp->Event->findByid($id);
            
            $total_capacity = $event['Event']['capacity'];

            $rsvpExists = $this->Rsvp->find ('count',array('conditions'=> array('user_id'=>$user_id,'event_id'=>$id)));

            if ($rsvpExists != 0){

                $rsvp_event = $this->Rsvp->find ('first',array('conditions'=> array('user_id'=>$user_id,'event_id'=>$id)));

                $register_nums = $this->Rsvp->find('first',array('fields' => array('COUNT(*)+sum(num_of_guests)as registered'),'conditions' => array('Rsvp.event_id'=>$id)));

                $register_nums = $register_nums[0]['registered'];

                $avail_capacity = $total_capacity - $register_nums;

                if($avail_capacity+$rsvp_event['Rsvp']['num_of_guests'] >= $n_guests){

                    $this->Rsvp->id = $rsvp_event['Rsvp']['id'];

                    $date = date('Y-m-d H:i:s');

                    $this->Rsvp->saveField('rsvp_ts', $date);
                    $this->Rsvp->saveField('rsvp_status', $attendance);

                    if ($attendance == 'N'){
                        $this->Rsvp->saveField('num_of_guests', 0);
                    }else{
                        $this->Rsvp->saveField('num_of_guests', $n_guests);
                    }
                    

                    $this->Session->setFlash(__('Your RSVP information has been updated.', true));

                    $this->redirect(array('controller' => 'events','action' => 'view',$ev_type,$id));
                }
                else{

                    $this->Session->setFlash(__('There are only '.$avail_capacity.' slots left.', true));

                    $this->redirect(array('controller' => 'events','action' => 'view',$ev_type,$id));
                }

            }else{

                if ($total_capacity >= ($n_guests + 1)){

                    $date = date('Y-m-d H:i:s');

                    $rsvp_data = array();

                    $rsvp_data['Rsvp']['rsvp_status'] = $attendance;

                    if ($attendance == 'N'){
                        $rsvp_data['Rsvp']['num_of_guests'] = 0;
                    }else{
                        $rsvp_data['Rsvp']['num_of_guests'] = $n_guests;
                    }
                    
                    $rsvp_data['Rsvp']['rsvp_ts'] = $date;
                    $rsvp_data['Rsvp']['event_id'] = $id;
                    $rsvp_data['Rsvp']['user_id'] = $user_id;

                    if ($this->Rsvp->save($rsvp_data)){
                        $this->Session->setFlash(__('Your RSVP information has been saved.', true));
                    }else{
                        $this->Session->setFlash(__('Your RSVP information could not be saved. Please try again.', true));
                    }

                    $this->redirect(array('controller' => 'events','action' => 'view',$ev_type,$id));
                }
                else{

                    $this->Session->setFlash(__('There are only '.$total_capacity.' slots left.', true));

                    $this->redirect(array('controller' => 'events','action' => 'view',$ev_type,$id));
                }

            }


        }
}
?>